<?php
/**
 *
 *
 * @author Zeon <scplay@gmail.com>
 * @date 2018/8/28 16:40
 * @copyright PanxSoft Inc.
 */

namespace App\Http\Controllers\WebPage\Seo;


use App\Http\Controllers\Controller;
use App\Http\Models\Admin\Product;
use App\Http\Models\Admin\ProductCategory;
use App\Http\Requests\WebPage\Seo\ProductIndexRequest;
use App\Services\Repositories\ProductCategoryRepository;
use App\Services\Repositories\ProductionRepository;
use Jaybizzle\CrawlerDetect\CrawlerDetect;

class ProductionController extends Controller
{
    const ROUTES = [
        'index' => 'seo-production.index',
        'show' => 'seo-production.show',
    ];

    const INDEX_VIEW = 'official_website.production';
    
    /**
     * @param ProductIndexRequest       $request
     * @param ProductionRepository      $prodRepo
     * @param ProductCategoryRepository $cateRepo
     * @return mixed
     * @throws \App\Exceptions\AdminManageException
     */
    public function index(ProductIndexRequest $request,
                          ProductionRepository $prodRepo,
                          ProductCategoryRepository $cateRepo)
    {
        if (! (new CrawlerDetect)->isCrawler()) return view(self::INDEX_VIEW);

        if ($request->detail_id) {
            $product = $prodRepo->findOrFail($request->detail_id);
            $title = $product->title . ' - '. $product->model_number;
            $keywords = $product->keywords;
            $description = $product->description;
        } else {
            $production = $prodRepo->index($request->pid, $request->keyword);
    
            if ($production->count()) {
                /** @var ProductCategory $product */
                $cate = $production->first()->productCategory;
                $title = $cate->name;
                $keywords = $cate->description;
                $description = $cate->description;
            }
        }
        
        // seo 分类导航
        $product_categories = $cateRepo->indexWithSub();
        
        return view(self::INDEX_VIEW)
            ->with(compact('production', 'product',
                'title', 'keywords', 'description', 'product_categories'));
    }
}